﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Salon
{
    public partial class RegisterTransfer : Form
    {
        public RegisterTransfer()
        {
            InitializeComponent();
            dgvTransfers.AutoGenerateColumns = false;
        }

        private void txtCar_DoubleClick(object sender, EventArgs e)
        {
            RegisterCars regcar = new RegisterCars(true);
            regcar.ShowDialog();
            if (regcar.ChosenCar != null)
            {
                txtCar.Text = regcar.ChosenCar.Id.ToString();
            }
        }

        private void RegisterTransfer_Load(object sender, EventArgs e)
        {
           
            IList<DictionaryItem> states = new List<DictionaryItem>();
            foreach (TransferState item in Enum.GetValues(typeof(TransferState)))
            {
                if (item != TransferState.New)
                    states.Add(new DictionaryItem((int)item,Global.GetEnumDescription(item)));
            }
            cmbStatus.DataSource = states;
            cmbStatus.SelectedItem = null;
            cmbSalonRec.Initialize();
            cmbSalonSen.Initialize();
            fillTable();
        }

        private void fillTable()
        {
            BindingSource bs = new BindingSource();
            TransferService ts = ServiceFactory.getTransferService();
            IList<Transfer> trans = ts.getTransfers(Global.getFilterList(this));
            foreach (Transfer tr in trans)
                bs.Add(tr);

            dgvTransfers.DataSource = bs;
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            fillTable();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            foreach (IFilter filtr in Global.getFilterList(this))
            {
                filtr.CleanUp();
            }
        }

        private void btnAddUser_Click(object sender, EventArgs e)
        {
            Global.MainPage.openNewTransferForm(null);
        }

        private void btnEditUser_Click(object sender, EventArgs e)
        {
            if (dgvTransfers.SelectedRows.Count != 1)
            {
                MessageBox.Show("Proszę zaznaczyć dokładnie jeden wiersz do edycji.");
                return;
            }

            Global.MainPage.openNewTransferForm((Transfer)dgvTransfers.SelectedRows[0].DataBoundItem);
        
        }

        private void btnDeleteUser_Click(object sender, EventArgs e)
        {
            if (dgvTransfers.SelectedRows.Count < 1)
            {
                MessageBox.Show("Proszę zaznaczyć przynajmniej jeden wiersz do usunięcia.");
                return;
            }

            if (MessageBox.Show("Czy na pewno chcesz usunąć wszystkie zaznaczone transfery?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                TransferService ts = new TransferService();
                for (int i = 0; i < dgvTransfers.SelectedRows.Count; i++)
                    ts.deleteTransfer((Transfer)dgvTransfers.SelectedRows[i].DataBoundItem);
                fillTable();
            }
        }

        private void dgvTransfers_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            FormTransfer ft = new FormTransfer((Transfer)dgvTransfers.Rows[e.RowIndex].DataBoundItem);
            ft.MdiParent = Global.MainPage;
            ft.Text = Forms.TRANSFER_FORM;
            ft.Show();
        }
    }
}
